package com.mvp.shq.washer.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mvp.shq.washer.R;
import com.mvp.shq.washer.entity.shop.Product;
import com.mvp.shq.washer.utils.DataDictionary;
import com.mvp.shq.washer.utils.ImageUtil;
import com.mvp.shq.washer.utils.StringUtils;

import java.math.BigDecimal;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by shq1 on 2017/9/30.
 */

public class ProductAdapter extends RecyclerView.Adapter<ProductAdapter.ViewHolder> {

    private Context context;
    private OnProductItemClickListener listener;
    private List<Product> products;
    private ImageUtil imageUtil;

    public ProductAdapter(Context mContext,List<Product> list) {
        context = mContext;
        products = list;
        imageUtil = new ImageUtil(context);
    }

    public void setListener(OnProductItemClickListener listener) {
        this.listener = listener;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_product, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Product product = products.get(position);
        BigDecimal decimal = new BigDecimal("0");
        int type = 0;
        String money1 = "",money2 = "",integral = "";
        if (product.getPayMode().equals(DataDictionary.PAY_MODE_CASH_AND_INTEGRAL)){
            type = 0;
            money1 = (int)product.getCashPrice()+"";
            money2 = StringUtils.BigDecimalToStr(BigDecimal.valueOf(product.getCashPrice()));
            integral = (int)product.getIntegralPrice()+"";
        }else if (product.getPayMode().equals(DataDictionary.PAY_MODE_CASH)){
            type = 2;
            money1 = (int)product.getCashPrice()+"";
            money2 = StringUtils.BigDecimalToStr(BigDecimal.valueOf(product.getCashPrice()));
        }else {
            type = 1;
            integral = (int)product.getIntegralPrice()+"";
        }
        switch (type){
            case 0:
                //现金加积分购买
                holder.llMoney.setVisibility(View.VISIBLE);
                holder.tvAdd.setVisibility(View.VISIBLE);
                holder.llIntegral.setVisibility(View.VISIBLE);
                holder.tvIntegralPrompt.setVisibility(View.GONE);
                holder.tvMoney1.setText(money1);
                holder.tvMoney2.setText(money2);
                holder.tvIntegral.setText(integral);
                break;
            case 1:
                //积分购买
                holder.llMoney.setVisibility(View.GONE);
                holder.tvAdd.setVisibility(View.GONE);
                holder.llIntegral.setVisibility(View.VISIBLE);
                holder.tvIntegralPrompt.setVisibility(View.VISIBLE);
                holder.tvIntegral.setText(integral);
                break;
            case 2:
                //现金购买
                holder.llMoney.setVisibility(View.VISIBLE);
                holder.tvAdd.setVisibility(View.GONE);
                holder.llIntegral.setVisibility(View.GONE);
                holder.tvIntegralPrompt.setVisibility(View.GONE);
                holder.tvMoney1.setText(money1);
                holder.tvMoney2.setText(money2);
                break;
        }

        holder.tvProductTitle.setText(product.getProductName());
        holder.tvProductType.setText(product.getSpecText());
        holder.tvByNum.setText(product.getSellNum() + "人已购买");
        imageUtil.loadUrlImage(product.getImgUrl(),holder.imgProductPic);
        holder.itemProduct.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null){
                    listener.onClick(position);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return products.size();
    }


    class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.img_product_pic)
        ImageView imgProductPic;
        @BindView(R.id.tv_product_title)
        TextView tvProductTitle;
        @BindView(R.id.tv_product_type)
        TextView tvProductType;
        @BindView(R.id.product_send_type)
        TextView productSendType;
        @BindView(R.id.tv_money_1)
        TextView tvMoney1;
        @BindView(R.id.tv_money2)
        TextView tvMoney2;
        @BindView(R.id.ll_money)
        LinearLayout llMoney;
        @BindView(R.id.tv_add)
        TextView tvAdd;
        @BindView(R.id.tv_integral)
        TextView tvIntegral;
        @BindView(R.id.ll_integral)
        LinearLayout llIntegral;
        @BindView(R.id.tv_by_num)
        TextView tvByNum;
        @BindView(R.id.item_product)
        LinearLayout itemProduct;
        @BindView(R.id.tv_integral_prompt)
        TextView tvIntegralPrompt;
        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
    }

    public interface OnProductItemClickListener{
        void onClick(int position);
    }
}
